package br.com.yk.client.ui;

import java.util.List;

import br.com.siam.pojo.Device;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;

public class UserListForm extends Window {
	
	public UserListForm() {
		super();
		createComponents();
	}
	private void createComponents() {
		setTitle("Usuários");
		setIconCls("paste-icon");  
		setMaximizable(true);
		setResizable(true);  
		setLayout(new FitLayout());  
		setWidth(500); 
		setHeight(600);
		setModal(true);
		BorderLayoutData topData = new BorderLayoutData(RegionPosition.NORTH);  
        topData.setMargins(3, 0, 3, 3);  
		add(createToolbar(), topData);
	}
	/**
	 * 
	 */
	private Panel createToolbar() {
		Panel panel = new Panel();
        Toolbar toolbar = new Toolbar();
        
        //new button
        ToolbarButton newObject = new ToolbarButton();
        newObject.setIcon("images/details.png");  
        newObject.setCls("x-btn-icon");  
        newObject.setTooltip("<b>Devices</b><br/>Lista dispositivos");
        newObject.addListener(new ButtonListenerAdapter() {
        	@Override
        	public void onClick(Button button, EventObject e) {
        		// TODO
        	}
        });
        toolbar.addButton(newObject);

        //edit button
        ToolbarButton editObject = new ToolbarButton();
        editObject.setIcon("images/details.png");  
        editObject.setCls("x-btn-icon");  
        editObject.setTooltip("<b>Devices</b><br/>Lista dispositivos");
        editObject.addListener(new ButtonListenerAdapter() {
        	@Override
        	public void onClick(Button button, EventObject e) {
        		// TODO
        	}
        });
        toolbar.addButton(editObject);
        
        //delete button
        ToolbarButton deleteObject = new ToolbarButton();
        deleteObject.setIcon("images/details.png");  
        deleteObject.setCls("x-btn-icon");  
        deleteObject.setTooltip("<b>Exclui o usuário</b>");
        deleteObject.addListener(new ButtonListenerAdapter() {
        	@Override
        	public void onClick(Button button, EventObject e) {
        		// TODO
        	}
        });
        toolbar.addButton(deleteObject);
        panel.add(toolbar);
        return panel;		
	}
	/**
	 * 
	 */
	public void init(List<Device> userList) {
        final RecordDef recordDef = new RecordDef(  
                new FieldDef[]{  
                		new IntegerFieldDef("id"),
                        new StringFieldDef("name")  
                }  
        );  
  
        final GridPanel grid = new GridPanel();
        grid.setLayout(new FitLayout());

        ColumnConfig[] columns = new ColumnConfig[]{  
                //column ID is company which is later used in setAutoExpandColumn  
                new ColumnConfig("Código", "id", 35),  
                new ColumnConfig("Nome", "name", 160, true, null, "name")  
        };  
  
        ColumnModel columnModel = new ColumnModel(columns);  
        grid.setColumnModel(columnModel);  
  
        grid.setFrame(true);  
        grid.setStripeRows(true);  
        grid.setAutoExpandColumn("name");  
  
        grid.setHeight(350);  
        grid.setWidth(600);  
        grid.setTitle("Dispositivos");  
  
        Toolbar bottomToolbar = new Toolbar();  
        bottomToolbar.addFill();  
        bottomToolbar.addButton(new ToolbarButton("Limpar Ordem", new ButtonListenerAdapter() {  
            public void onClick(Button button, EventObject e) {  
                grid.clearSortState(true);  
            }  
        }));  
        grid.setBottomToolbar(bottomToolbar);  
  
        Object[][] data = getUserData(userList);
        MemoryProxy proxy = new MemoryProxy(data);  
        ArrayReader reader = new ArrayReader(recordDef);  
        Store store = new Store(proxy, reader);  
        store.load();  
        grid.setStore(store);

		BorderLayoutData topData = new BorderLayoutData(RegionPosition.CENTER);  
        topData.setMargins(3, 0, 3, 3);
        Panel panel = new Panel();
        panel.add(grid);
		add(panel, topData);
	}
	/**
	 * @param userList
	 * @return
	 */
	private Object[][] getUserData(List<Device> userList)  {
		Object[][] result = new Object[userList.size()] [2];
		int i = 0;
		for (Device item : userList) {
			result[i][0] = item.getId();
			result[i][1] = item.getName();
			i++;
		}
		return result;
	}
}
